package com.yanqu.road.server.manger.activity.washe;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.entity.enums.eSeasonRewardType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.enums.rank.eRankInfo;
import com.yanqu.road.logic.helper.SeasonHelper;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.cross3.ChannelRankActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ChannelProtocol;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ChannelRankReloadMgr extends TempMgr {

    /**
     * 让跨域把 [瓦舍赛季总榜单] 同步到本渠道跨服
     */
    public static void reloadWasheSeasonRank() {
        WasheConfig washeConfig = WasheMgr.getWasheConfig();
        if (washeConfig != null && washeConfig.getSeasonId() > WasheConfig.SEASON_ID_TEST) {
            XRankProto.ChannelSyncXSeasonUserRankListReqMsg.Builder builder = XRankProto.ChannelSyncXSeasonUserRankListReqMsg.newBuilder();
            builder.setSystemId(eSystemId.WasheActivity.getValue());
            builder.setSeasonId(washeConfig.getSeasonId());
            builder.setType(eSeasonRewardType.WasheScoreRank.getIntValue());
            builder.setRangeKey(SeasonHelper.getWasheRangeKey(washeConfig.getActivityInfo().getParam6()));
            builder.setChannelId(ServerListMgr.getChannelId(GameServer.getInstance().getServerId()));
            GamePlayerMgr.sendChannelPacket(ChannelProtocol.N_XRANK_REQ_SYNC_SEASON_RANK_LIST, builder, 0);
        }
    }

    /**
     * 让跨域把 [跨域冲榜活动榜单] 同步到本渠道跨服
     */
    public static void reloadChannelRank() {
        for (ActivityInfo activityInfo : ChannelRankActivityMgr.getInfoMap().values()) {
            if (!ActivityMgr.activityInShowTime(activityInfo)) {
                continue;
            }

            // 获取所有conditionType
            Set<Integer> typeSet = new HashSet<>();
            List<ActivityConditionInfo> list = NormalActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId());
            if (list != null) {
                for (ActivityConditionInfo conditionInfo : list) {
                    typeSet.add(conditionInfo.getType());
                }
            }

            // 找出是榜单的类型
            for (int type : typeSet) {
                eRankInfo info = eRankInfo.getERankInfo(activityInfo.getType(), activityInfo.getChildType(), type);
                if (info != null) {

                    // 发消息到跨域
                    XRankProto.ChannelSyncXRankListReqMsg.Builder builder = XRankProto.ChannelSyncXRankListReqMsg.newBuilder();
                    builder.setActivityId(activityInfo.getActivityId());
                    builder.setConditionType(type);
                    builder.setChannelId(ServerListMgr.getChannelId(GameServer.getInstance().getServerId()));
                    builder.setRange(info.getRangeType().getValue());
                    GamePlayerMgr.sendChannelPacket(ChannelProtocol.N_XRANK_REQ_SYNC_RANK_LIST, builder, 0);
                }
            }
        }
    }

    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        reloadWasheSeasonRank();
        reloadChannelRank();
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
